package com.hnkywl.sts.action.sys;

import com.hnkywl.sts.entity.sys.*;
import com.hnkywl.sts.service.organ.GsjgService;
import com.hnkywl.sts.service.sys.WzbmService;
import com.hnkywl.sts.service.sys.YgService;
import jxl.Sheet;
import jxl.Workbook;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.hibernate.tool.hbm2x.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 说明：
 * 包:        com.hnkywl.sts.action.sys
 * 类描述:    物资编码Action类
 * 创建人:    ccr
 * 创建时间: 13-2-18 上午16:09
 * 修改人:    ccr
 * 修改时间: 13-2-18 上午16:09
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Namespace("/sys/wzbm")
public class WzbmAction extends SimpleActionSupport {
    private WzbmService wzbmService;
    //一级物资实例
    private Yjwzbm yjwzbm;
    //二级物资实例
    private Rjwzbm rjwzbm;
    //品名
    private Pm pm;
    //规格
    private Gg gg;
    //传入的id值
    private Long wzbmId;
    //上级物资编码ID
    private Long parentId;
    //层级,0 顶层,1 一级物资编码,2 二级物资编码 3 品名 4 规格
    private int level;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows;
    //行索引
    private int index;
    //查询条件 name
    private String name;
    //查询条件 code
    private String code;
    //查询条件 材质
    private String cz;
    //当前公司Id
    private Long gsId;

    private String ggxh;

    private Integer fw;

    private String codeId;

    private YgService ygService;
    private Yg yg;
    private String currentUser;
    private Long ygId;
    private Boolean isYgBmfwSetted;


    private File upload;


    /**
     * 调用仓储新建物资编码页面
     *
     * @return
     */
    @Action("manageMaterial")
    public String manageMaterial() {
        yg = ygService.findBygh("manageMaterial");
        currentUser = YgService.getCurrentYgName();
        return SUCCESS;
    }

    /**
     * 方法名:     getTreeJson
     * 功能描述:   生成树Json串
     * 输入参数:   <@param wzbmId level>
     * 异常:      <RuntimeException  Exception>
     * 创建人:     ccr
     * 创建日期:   13-2-20 下午15:47
     * 修改人:     ccr
     * 修改日期:   13-2-20 下午15:47
     */
    @Action("get_treejson")
    public void getTreeJson() {
        if (request.getParameter("id") == null) {
            StringBuilder sb = new StringBuilder("[{\"id\": \"0:0\", \"text\": \"编码物资\", \"attributes\": {\"level\": \"0\", \"children\": \"false\"}, \"state\": \"open\",\"children\":");
            String treeJson = wzbmService.getWzbmTreeJson(0l, 0);
            sb.append(treeJson).append("}]");
            renderHtml(sb.toString());
        } else {
            String[] values = request.getParameter("id").split(":");
            wzbmId = Long.valueOf(values[0]);
            level = Integer.valueOf(values[1]);
            renderText(wzbmService.getWzbmTreeJson(wzbmId, level));
        }
    }

    /**
     * 加载所有品名根据Codeid
     */
    @Action("get_treejsonbycodeid")
    public void getTreeJsonByCodeid() {
        renderText(wzbmService.getWzbmTreeJsonByCodeid(codeId));
    }

    /**
     * 方法名:     getTreeJson
     * 功能描述:   生成树Json串
     * 输入参数:   <@param wzbmId level>
     * 异常:      <RuntimeException  Exception>
     * 创建人:     ccr
     * 创建日期:   13-2-20 下午15:47
     * 修改人:     ccr
     * 修改日期:   13-2-20 下午15:47
     */
    @Action("get_gridjson")
    public void getGridJson() {
        try {
//            gsId = YgService.getCurrentYgGsId();
            renderJson(wzbmService.getGridJson(gsId, wzbmId, level, page, rows).replaceAll("\r", ""));
        } catch (InvocationTargetException e) {
            e.printStackTrace();  //反射和产生的异常当缺少getMessage()传入的e极为null  必须处理
        } catch (NoSuchMethodException e) {
            e.printStackTrace();  //Action中访问类型被定义为private，action中的类一定是public
        } catch (IllegalAccessException e) {
            e.printStackTrace();  //指针异常引起的，一般为空值针异常，或者数组下表越界异常
        }
    }

    @Action("search")
    public void search() {
        renderJson(wzbmService.search(pm == null ? null : pm.getName(), name, code, cz, ggxh, gsId, fw, page, rows));
    }

    @Actions({@Action("yjwzbm_content"), @Action("search_yjwzbm_content")})
    public String yjwzbmContent() {
        if (wzbmId != null)
            yjwzbm = wzbmService.getYjwzbm(wzbmId);
        return SUCCESS;
    }

    @Action("wzbm")
    public String wzbm() {
        gsId = YgService.getCurrentYgGsId();
        return SUCCESS;
    }

    @Action("search_wzbm")
    public String searchWzbm() {
        return SUCCESS;
    }

    /**
     * 方法名:     增添一级物资编码
     * 功能描述:      向数据库插入一级物资编码
     * 输入参数:    <@param yjwzbm 一级物资实例>
     * 异常:      <RuntimeException  Exception>
     * 创建人:     hfj
     * 创建日期:   13-2-19上午10:47
     * 修改人:     hfj
     * 修改日期:   13-2-19上午10:47
     */
    @Action("add_yjwzbm")
    public void addYjwzbm() {
        try {
            wzbmService.saveYjwzbm(yjwzbm);
            renderText("{\"statusCode\":\"200\", \"message\":\"添加一级物资编码成功\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 方法名:     删除一级物资
     * 功能描述:      通过id删除数据库中的一级物资
     * 输入参数:    <@param box 页面传入的id值集合>
     * 异常:      <RuntimeException  Exception>
     * 创建人:     hfj
     * 创建日期:   13-2-19上午10:47
     * 修改人:     hfj
     * 修改日期:   13-2-19上午10:47
     */
    @Action("delete_yjwzbm")
    public void delYjwzbm() {
        try {
            wzbmService.deleteYjwzbm(wzbmId);
            renderText("{\"statusCode\":\"200\", \"message\":\"删除一级物资编码成功\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 方法名:     更新一级物资编码
     * 功能描述:      向数据库更新一级物资编码
     * 输入参数:    <@param Yjwzbm 一级物资>
     * 异常:      <RuntimeException  Exception>
     * 创建人:     hfj
     * 创建日期:   13-2-19上午10:47
     * 修改人:     hfj
     * 修改日期:   13-2-19上午10:47
     */
    @Action("update_yjwzbm")
    public void updateYjwz() {
        try {
            wzbmService.updateYjwzbm(yjwzbm);
            renderText("{\"statusCode\":\"200\",\"message\":\"修改成功\",\"operationType\":\"update\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Actions({@Action("rjwzbm_content"), @Action("search_rjwzbm_content")})
    public String rjwzbmContent() {
        if (wzbmId != null)
            rjwzbm = wzbmService.getRjwzbm(wzbmId);
        else {
            rjwzbm = new Rjwzbm();
            rjwzbm.setYjwzbm(this.wzbmService.getYjwzbm(this.parentId));
        }
        return SUCCESS;
    }

    /**
     * 添加二级物资编码
     */
    @Action("add_rjwzbm")
    public void addRjwzbm() {
        try {
            wzbmService.save(rjwzbm);
            renderText("{\"statusCode\":\"200\", \"message\":\"添加二级物资编码成功\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 更新二级物资编码
     */
    @Action("update_rjwzbm")
    public void updateRjwzbm() {
        try {
            wzbmService.update(rjwzbm);
            renderText("{\"statusCode\":\"200\", \"message\":\"修改成功\", \"operationType\":\"update\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 删除二级物资编码
     */
    @Action("delete_rjwzbm")
    public void deleteRjwzbm() {
        try {
            wzbmService.delete(wzbmId);
            renderText("{\"statusCode\":\"200\", \"message\":\"删除二级物资编码成功\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Actions({@Action("pm_content"), @Action("search_pm_content")})
    public String pmContent() {
        if (wzbmId != null)
            pm = wzbmService.getPm(wzbmId);
        else {
            pm = new Pm();
            pm.setRjwzbm(this.wzbmService.getRjwzbm(this.parentId));
        }
        return SUCCESS;
    }

    /**
     * 添加一个品名的方法
     */
    @Action("add_pm")
    public void addPm() {
        try {
            wzbmService.save(pm);
            renderText("{\"statusCode\":\"200\", \"message\":\"添加品名成功\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 跟新品名的方法
     */
    @Action("update_pm")
    public void updatePm() {
        try {
            wzbmService.updatePm(pm);
            renderText("{\"statusCode\":\"200\", \"message\":\"修改品名成功\", \"operationType\":\"update\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 删除品名的方法
     */
    @Action("delete_pm")
    public void deletePm() {
        try {
            wzbmService.deletePm(wzbmId);
            renderText("{\"statusCode\":\"200\", \"message\":\"删除品名成功\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Actions({@Action("gg_content"), @Action("search_gg_content")})
    public String ggContent() {
        if (wzbmId != null)
            gg = wzbmService.getGg(wzbmId);
        else {
            gg = new Gg();
            gg.setPm(this.wzbmService.getPm(this.parentId));
        }
        return SUCCESS;
    }

    /**
     * 添加规格
     */
    @Action("add_gg")
    public void add() {
        try {
            wzbmService.save(gg);
            renderText("{\"statusCode\":\"200\", \"message\":\"添加品名成功\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 修改规格
     */
    @Action("update_gg")
    public void update() {
        try {
            wzbmService.update(gg);
            renderText("{\"statusCode\":\"200\", \"message\":\"修改规格成功\", \"operationType\":\"update\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 删除规格
     */
    @Action("delete_gg")
    public void delete() {
        try {
            wzbmService.deleteGg(wzbmId);
            renderText("{\"statusCode\":\"200\", \"message\":\"删除规格成功\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("findAllYjwzbm")
    public void yjwzbm() {
        renderJson(wzbmService.findAllYjwzbmCode());
    }


    @Action("search_wzbm_yg")
    public String searchWzbmYg() {
        ygId = YgService.getCurrentYg().getId();
        isYgBmfwSetted = wzbmService.isYgBmfwSetted(YgService.getCurrentYg().getId());
        return SUCCESS;
    }

    @Action("wzbm_import")
    public void wzbmImport() {
        try {
            Workbook book = Workbook.getWorkbook(upload);
            //  获得第一个工作表对象
            Sheet sheet = book.getSheet(0);
            //获取总行数
            int rows = sheet.getRows();
            //品名的名字
            String pmName = wzbmService.getPm(Long.parseLong(code)).getName();
            List<Gg> ggList = new ArrayList<Gg>();

            for (int i = 1; i < rows; i++) {//从第2行开始导入
                //在exel文件中查找与页面传过来的品名相同的品名
                if (pmName.equals(sheet.getCell(0, i).getContents())) {

                    gg = new Gg();

                    //第一列 品种
                    if (sheet.getCell(0, i) == null && StringUtils.isEmpty(sheet.getCell(0, i).getContents())) {
                       /* gg.setName(pmName);
                        gg.setPm(wzbmService.getPm(Long.parseLong(code)));
                    } else {*/
                        renderText("{\"statusCode\":\"300\", \"message\":\"第" + (i + 1) + "行数据品种为空！\"}");
                        return;
                    }
                    //第二列 材质
                    if(sheet.getCell(1, i)==null || StringUtils.isEmpty(sheet.getCell(1, i).getContents())){
                       /* gg.setCz(sheet.getCell(1, i).getContents());
                    } else {*/
                        renderText("{\"statusCode\":\"300\", \"message\":\"第" + (i + 1) + "行数据材质为空！\"}");
                        return;
                    }
                      //第三列 厚度
                    if(sheet.getCell(2, i)==null || StringUtils.isEmpty(sheet.getCell(2, i).getContents())){
                     /*   String ggxh = sheet.getCell(2, i).getContents() + sheet.getCell(3, i).getContents() +sheet.getCell(4, i).getContents()+sheet.getCell(5, i).getContents()+sheet.getCell(6, i).getContents();
                        gg.setGgxh(ggxh);
                    } else {*/
                        renderText("{\"statusCode\":\"300\", \"message\":\"第" + (i + 1) + "行数据为空！\"}");
                        return;
                    }


                    //第四列
                    if(sheet.getCell(3, i)==null || StringUtils.isEmpty(sheet.getCell(3, i).getContents())){
                    /*    String ggxh = sheet.getCell(2, i).getContents() + sheet.getCell(3, i).getContents() +sheet.getCell(4, i).getContents()+sheet.getCell(5, i).getContents()+sheet.getCell(6, i).getContents();
                        gg.setGgxh(ggxh);
                    } else {*/
                        renderText("{\"statusCode\":\"300\", \"message\":\"第" + (i + 1) + "规格型号为空\"}");
                        return;
                    }


                    //第五列
                    if(sheet.getCell(4, i)==null || StringUtils.isEmpty(sheet.getCell(4, i).getContents())){
                  /*      String ggxh = sheet.getCell(2, i).getContents() + sheet.getCell(3, i).getContents() +sheet.getCell(4, i).getContents()+sheet.getCell(5, i).getContents()+sheet.getCell(6, i).getContents();
                        gg.setGgxh(ggxh);
                    } else {*/
                        renderText("{\"statusCode\":\"300\", \"message\":\"第" + (i + 1) + "规格型号为空\"}");
                        return;
                    }

                    //第六列
                    if(sheet.getCell(5, i)==null || StringUtils.isEmpty(sheet.getCell(5, i).getContents())){
                     /*   String ggxh = sheet.getCell(2, i).getContents() + sheet.getCell(3, i).getContents() +sheet.getCell(4, i).getContents()+sheet.getCell(5, i).getContents()+sheet.getCell(6, i).getContents();
                        gg.setGgxh(ggxh);
                    } else {*/
                        renderText("{\"statusCode\":\"300\", \"message\":\"第" + (i + 1) + "规格型号为空\"}");
                        return;
                    }

                    //第六列
                    if(sheet.getCell(6, i)==null || StringUtils.isEmpty(sheet.getCell(6, i).getContents())){
                    /*    String ggxh = sheet.getCell(2, i).getContents() + sheet.getCell(3, i).getContents() +sheet.getCell(4, i).getContents()+sheet.getCell(5, i).getContents()+sheet.getCell(6, i).getContents();
                        gg.setGgxh(ggxh);
                    } else {*/
                        renderText("{\"statusCode\":\"300\", \"message\":\"第" + (i + 1) + "规格型号为空\"}");
                        return;
                    }

                    gg.setName(pmName);
                    gg.setDw("吨");
                    gg.setPm(wzbmService.getPm(Long.parseLong(code)));
                    gg.setCz(sheet.getCell(1, i).getContents());
                    String ggxh = sheet.getCell(2, i).getContents() + sheet.getCell(3, i).getContents() +sheet.getCell(4, i).getContents()+sheet.getCell(5, i).getContents()+sheet.getCell(6, i).getContents();
                    gg.setGgxh(ggxh);
                    ggList.add(gg);
                }
            }

            if (ggList.size() > 0) {
                this.wzbmService.saveImportWz(ggList);
                renderText("{\"statusCode\":\"200\", \"message\":\"保存成功！\"}");
            } else {
                renderText("{\"statusCode\":\"300\", \"message\":\"表格中没有要导入的品名\"}");
            }

        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"数据异常：" + e.getMessage().replace("\"", " ") + "\"}");
        }
    }

    //页面跳转
    @Action("wzbm_upload")
    public String wzbmUpload() {
        //品名
        pm = this.wzbmService.getPm(this.parentId);
        return SUCCESS;
    }


    //============getter setter============


    public Long getYgId() {
        return ygId;
    }

    public void setYgId(Long ygId) {
        this.ygId = ygId;
    }

    public Boolean getIsYgBmfwSetted() {
        return isYgBmfwSetted;
    }

    public void setIsYgBmfwSetted(Boolean isYgBmfwSetted) {
        this.isYgBmfwSetted = isYgBmfwSetted;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCz() {
        return cz;
    }

    public void setCz(String cz) {
        this.cz = cz;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public Long getWzbmId() {
        return wzbmId;
    }

    public void setWzbmId(Long wzbmId) {
        this.wzbmId = wzbmId;
    }

    public Yjwzbm getYjwzbm() {
        return yjwzbm;
    }

    public void setYjwzbm(Yjwzbm yjwzbm) {
        this.yjwzbm = yjwzbm;
    }

    public Rjwzbm getRjwzbm() {
        return rjwzbm;
    }

    public void setRjwzbm(Rjwzbm rjwzbm) {
        this.rjwzbm = rjwzbm;
    }

    public Pm getPm() {
        return pm;
    }

    public void setPm(Pm pm) {
        this.pm = pm;
    }

    public Gg getGg() {
        return gg;
    }

    public void setGg(Gg gg) {
        this.gg = gg;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public String getGgxh() {
        return ggxh;
    }

    public void setGgxh(String ggxh) {
        this.ggxh = ggxh;
    }

    public Integer getFw() {
        return fw;
    }

    public void setFw(Integer fw) {
        this.fw = fw;
    }

    public String getCodeId() {
        return codeId;
    }

    public void setCodeId(String codeId) {
        this.codeId = codeId;
    }

    public Yg getYg() {
        return yg;
    }

    public void setYg(Yg yg) {
        this.yg = yg;
    }

    public String getCurrentUser() {
        return currentUser;
    }

    public void setCurrentUser(String currentUser) {
        this.currentUser = currentUser;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    //===============setter============
    @Autowired
    public void setWzbmService(WzbmService wzbmService) {
        this.wzbmService = wzbmService;
    }

    @Autowired
    public void setYgService(YgService ygService) {
        this.ygService = ygService;
    }
}
